package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Permission;
import cn.marinda.resturants.entity.User;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.PermissionMapper;
import cn.marinda.resturants.service.PermissionsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author hwj
 */
@Service("r_permissions")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission> implements PermissionsService {


    /**
     * 选择uid许可
     *
     * @param uid uid
     * @return {@link Permission}
     */
    @Override
    public Permission selectPermissionUid(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 分页User
     *
     * @param page 传输page数据
     * @return 迭代的page
     */
    @Override
    public IPage<Permission> selectPages(Pages<?> page) {
        return baseMapper.selectPages(page);
    }

    /**
     * 查询所有权限
     *
     * @return 权限列表
     */
    @Override
    public List<Permission> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 添加一个权限
     *
     * @param permission 权限
     * @return 结果
     */
    @Override
    public boolean addPermission(Permission permission) {
        return baseMapper.insert(permission)>0;
    }

    /**
     * 删除一个权限
     *
     * @param uid uid
     * @return 结果
     */
    @Override
    public boolean deletePermission(long uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 子权限
     *
     * @param uid uid
     * @return 子权限
     */
    @Override
    public long selectChildUid(long uid) {
        return baseMapper.selectChildUid(uid);
    }

    /**
     * 查询名字
     *
     * @param uid uid
     * @return 名字
     */
    @Override
    public String selectName(long uid) {
        return baseMapper.selectName(uid);
    }

    /**
     * 查询父权限
     *
     * @param uid uid
     * @return 父级权限
     */
    @Override
    public long selectParentUid(long uid) {
        return baseMapper.selectParentUid(uid);
    }

    /**
     * 乐观锁修改权限
     *
     * @param permission 权限
     * @return 结果
     */
    @Override
    public boolean updatePermission(Permission permission) {
        Permission sqlPermission = baseMapper.selectById(permission.getUid());
        if(sqlPermission == null){
            return false;
        }
        permission.setVersion(sqlPermission.getVersion());
        return lambdaUpdate().eq(Permission::getUid,permission.getUid()).eq(Permission::getVersion,permission.getVersion()).set(Permission::getVersion,permission.getVersion()+1).update(permission);
    }
}
